<?php
namespace app\api\controller\newfile;

use think\Request;
use app\common\model\newmodel\Customer as CustomerModel;
use app\common\model\power\Rolepower;
use app\common\model\newmodel\User as UserModel;
use app\common\model\newmodel\UserAgency;
use app\api\controller\Pub;
use app\api\controller\Base;
use app\common\model\newmodel\ContractOrder;
use app\common\model\newmodel\Device as DeviceModel;

class Orders extends Base
{
    public function initialize(){
		parent::initialize();
		self::is_login();
		Rolepower::powerguard(301002);
	}

    /**
	 * 判断账号是否登录
	 * @return boolean [description]
	 */
	private function is_login(){
		$data = UserModel::is_sessionlogin();
		if (!$data['code']) {
			$data['options'] = 'relogin';
			json($data)->send();
			exit;
		}
	}

    //产品列表
    public function productList()
    {
        $data=DeviceModel::deviceList();
        return myok($data);
    }

    //生成订单
    public function productOrder(Request $request)
    {
        $data = $request->post();
        $result = ContractOrder::createProductOrders($data);
        return json($result);
    }

    //订单列表
    public function orderList()
    {
        $userId = Pub::userid();
        $data=input();
        $page = input('page') ?? 1;
        $pageSize = input('pageSize') ?? 1;
        $keyword = input('keyword', '');
        $status = input('status') ?? '';
        $result = ContractOrder::getOrderList($userId, $page, $pageSize, $keyword ,$status);
        return json($result);
    }

    /**
     * 订单状态修改
     */
    public function orderStatusChange(Request $request)
    {
        $data = $request->post();
        if (empty($data['id'])) {
            return json(['code' => 0, 'msg' => '参数不完整']);
        }

        $result = ContractOrder::updateOrderStatus($data);
        return json($result);
    }
    /**
     * 根据合同查看对应的订单列表（一个合同对应多个订单）
     */
    public function getOrdersByContractId(Request $request)
    {
        $data=input();
        if(empty($data['contract_id'])){
            return json(['code' => 0, 'msg' => '参数不完整']);
        }
        $result=ContractOrder::getOrdersByContractId($data['contract_id']);
        return json($result);
    }
    
    /**
     * 订单删除
     */
    public function orderDel()
    {
        $order_id=input('id');
        $res=ContractOrder::where('id',$order_id)->update(['State'=>3]);
        if($res){
            return json([
                'code' =>1,
                'msg'  =>'删除成功'
            ]);
        }else{
            return json([
                'code' =>0,
                'msg'  =>'删除失败'
            ]);
        }
        
    }

}